<?php

namespace app\admin\controller;

use think\facade\App;

class AuthMember extends Base {

    /**
     * @desc 后台用户
     */
    public function index() {
        $params['name']   = input('name');
        $params['status']   = input('status');

        $logic = App::model('AuthMember', 'logic');
        $data = $logic->index($params);

        $this->assign($data);
        return view();
    }

    /**
     * @desc 添加后台用户
     */
    public function add() {
        $params['action'] = input('action');

        $logic = App::model('authMember', 'logic');
        $data = $logic->add($params);
        $this->assign($data);

        return view();
    }

    /**
     * @desc 编辑后台用户
     */
    public function edit() {
        $params['action']   = input('action');
        $params['memberId'] = input('memberId');

        $logic = App::model('authMember', 'logic');
        $data = $logic->edit($params);

        $this->assign($data);

        return view();
    }

    /**
     * @desc 用户操作 (添加、编辑)
     */
    public function operateAjax() {
        $params['action']   = input('action');
        $params['memberId'] = input('memberId', 0);
        $params['username'] = input('username');
        $params['password'] = input('password');
        $params['status']   = input('status');
        $params['groupId']  = input('groupId');
        $params['remark']   = input('remark');

        $logic = model('authMember', 'logic');
        $data = [];
        try {
            $data = $logic->operateAjax($params);
        } catch (\Exception $e) {
            $logic->errorCode = EC_FAILURE;
            $logic->message = $e->getMessage();
        }
        return send_response($data, $logic->errorCode, $logic->message);
    }
    
    /**
     * @desc 获取随机密码
     */
    public function getRandomPassword() {
        $password = random_string(32);
        return send_response(['password' => $password], EC_SUCCESS);
    }
}
